import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import { WeaTableNew } from 'comsMobx'
import * as API_LIST from "../apis/list";
const { TableStore } = WeaTableNew;

/**
 * @author liuzy 2017-10-18
 * 流程列表通用store模板，适应于树+列表+高级查询模式，实例：待办/已办/我的请求/查询流程/流程回收站等等
 */
export class ListStore{
    /** 列表store */
    @observable loading = false;
    title = "";
    @observable tableStore = new TableStore();

    /** 高级查询store */
    @observable form = new WeaForm();
    @observable showSearchAd = false;
    condition = [];

    /** 左侧树store */
    @observable leftTree = [];
    @observable leftTreeCount = {};
    leftTreeCountType = [];
    @observable selectedTreeKey = "";

    /** 顶部Tab组件store */
    @observable topTab = [];
    @observable topTabCount = {};

    constructor() {
        this.setLoading = this.setLoading.bind(this);
        this.setTitle = this.setTitle.bind(this);
        this.setShowSearchAd = this.setShowSearchAd.bind(this);
        this.setFormFields = this.setFormFields.bind(this);
        this.appendFormFields = this.appendFormFields.bind(this);
        this.clearFormFields = this.clearFormFields.bind(this);
        this.setSelectedTreeKey = this.setSelectedTreeKey.bind(this);
        this.resetForm = this.resetForm.bind(this);
        this.resetTable = this.resetTable.bind(this);
        this.rewriteTableStore();
    }

    rewriteTableStore = () =>{
        const getDatas = this.tableStore.getDatas.bind(this.tableStore);//重写组件getdatas方法，处理点击列排序、切页时不走回调
        this.tableStore.getDatas = (...arg) => {
            getDatas(arg[0], arg[1], arg[2], arg[3], () => {
                this.showUnoperators(arg[4]);
            }, arg[5]);
        }
    }

    showUnoperators(callback){
        if(callback && typeof(callback) === 'function'){
            callback();//原tablestore的callback
        }
        let newDatas = [];//[{requestid:1,userid:1,unoperators:1},{requestid:2,userid:2,unoperators:2}]
        if(this.tableStore.datas){
            this.tableStore.datas.map(data => {
                newDatas.push({"requestid":data.requestid,"userid":data.userid,"unoperators":data.unoperators});
            })
        }
        API_LIST.getUnoperators({datas:JSON.stringify(newDatas),actiontype:'batch',showAll:'false'}).then(unoperators =>{
            for(var unopt in unoperators){
                jQuery("#"+unopt).html(unoperators[unopt]);
            }
        });
    }

    @action
    setLoading(bool = false){
        this.loading = bool;
    }

    setTitle(title){
        this.title = title;
    }

    setShowSearchAd(bool) {
        this.showSearchAd = bool;
    }

    setFormFields(value){
        this.form.updateFields(value, true);    //true代表完全覆盖方式更新条件值
    }

    appendFormFields(value){
        this.form.updateFields(value, false);
    }

    clearFormFields(){
        this.form.resetConditionValue();      //清除查询条件值
    }

    setSelectedTreeKey(key) {
        this.selectedTreeKey = key;
    }

    resetForm(){
        this.form = new WeaForm();
    }

    resetTable(needNew=false){
        if(needNew) {
            this.tableStore = new TableStore();
            this.rewriteTableStore();
        }
        else
            this.tableStore.tableUpdate({datas: []});
    }

}